package cc.mallet.topics.gui;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import cc.mallet.pipe.Pipe;
import cc.mallet.types.Instance;
import cc.mallet.types.Token;
import cc.mallet.types.TokenSequence;

/**
 * 
 * @author Hiep.Hong
 * 
 */
public class TokenSequenceRemoveEmailHeader extends Pipe implements
		Serializable {

	public TokenSequenceRemoveEmailHeader() {
	}

	public Instance pipe(Instance carrier) {
		TokenSequence ts = (TokenSequence) carrier.getData();
		TokenSequence ret = new TokenSequence();

		for (int i = 0; i < ts.size(); i++) {
			Token token = ts.get(i);

			if ("Message".equals(token.getText())) {
				token = ts.get(i + 1);
				if ("ID".equals(token.getText())) {

					while (!("FileName".equals(token.getText()))) {
						i++;
						if (i < ts.size()) {
							token = ts.get(i);
						} else {
							break;
						}
					}
					if ("FileName".equals(token.getText())) {
						// Skip the name after "X-FileName:" token
						i = i + 4;
						continue;
					}
				}
			}

			System.out.print(token.getText() + " ");

			ret.add(token);
		}
		System.out.println();

		carrier.setData(ret);
		return carrier;
	}

	// Serialization

	private static final long serialVersionUID = 2014082601L;
	private static final int CURRENT_SERIAL_VERSION = 2;

	private void writeObject(ObjectOutputStream out) throws IOException {
		out.writeInt(CURRENT_SERIAL_VERSION);
	}

	private void readObject(ObjectInputStream in) throws IOException,
			ClassNotFoundException {
		int version = in.readInt();
		if (version > 0) {
		}
		if (version > 1) {
		}
	}

}
